export function conversion() {
	const t = new Date()
	const Y = t.getFullYear().toString().padStart(2, 0);
	const M = (t.getMonth() + 1).toString().padStart(2, 0);
	const D = t.getDate().toString().padStart(2, 0);
	const h = t.getHours().toString().padStart(2, 0);
	const m = t.getMinutes().toString().padStart(2, 0);
	const s = t.getSeconds().toString().padStart(2, 0);
	return {
		// 年月日
		nyr: Y + '-' + M + '-' + D,
		// 当前时间
		nowTime: Y + '-' + M + '-' + D + ' ' + h + ':' + m
	}
}

// 单纯的线路规划
export function getDataObj(points, didiPath) {
	let color = '#0091ff'
	return {
		// 起点
		origin: points[0].longitude + ',' + points[0].latitude,
		// 终点
		destination: points[1].longitude + ',' + points[1].latitude,
		success: data => {
			console.log(data, '驾车的路线', 26)
			didiPath.distance = (data.paths[0].distance / 1000).toFixed(1)
			didiPath.minute = Math.ceil(data.paths[0].duration / 60)
			didiPath.payMoney = parseFloat(data.taxi_cost).toFixed(2)
			// 驾车的路线
			let points = []
			if (!data || !data.paths || !data.paths[0] || !data.paths[0].steps) {
				uni.$showMsg('查询失败')
				return
			}
			let steps = data.paths[0].steps
			for (let i = 0; i < steps.length; i++) {
				let polylines = steps[i].polyline.split(';')
				for (let j = 0; j < polylines.length; j++) {
					let locations = polylines[j].split(',')
					points.push({
						longitude: locations[0],
						latitude: locations[1]
					})
				}
			}
			// 路线的规划
			didiPath.polyline = [{
				points,
				color,
				width: 3
			}]

			// if (didiPath.polyline[0].points.length > 2) {
			// 	// 改变起点
			// 	didiPath.points[0].longitude = didiPath.polyline[0].points[2].longitude
			// 	didiPath.points[0].latitude = didiPath.polyline[0].points[2].latitude
			// }
			// if (didiPath.polyline[0].points.length == 2) {
			// 	didiPath.carNum++
			// 	// 改变起点
			// 	didiPath.points[0].longitude = didiPath.polyline[0].points[1].longitude
			// 	didiPath.points[0].latitude = didiPath.polyline[0].points[1].latitude

			// 	if (didiPath.carNum == 2) {
			// 		didiPath.initialPoint[0].longitude = didiPath.polyline[0].points[0].longitude
			// 		didiPath.initialPoint[0].latitude = didiPath.polyline[0].points[0].latitude
			// 		// 重置
			// 		didiPath.carNum = 0
			// 		return
			// 	}

			// }
			// setTimeout(() => {
			// 	didiPath.initialPoint[0].longitude = didiPath.polyline[0].points[0].longitude
			// 	didiPath.initialPoint[0].latitude = didiPath.polyline[0].points[0].latitude

			// 	didiPath.amapPlugin.getDrivingRoute(getDataObj(didiPath.points, didiPath))
			// }, 500)
		},
		// 获取路线失败
		fail(info) {
			console.log('获取失败', info)
		}
	}
}

export function trendsPath(points, didiPath) {
	let color = '#0091ff'
	return {
		// 起点
		origin: points[0].longitude + ',' + points[0].latitude,
		// 终点
		destination: points[1].longitude + ',' + points[1].latitude,
		success: data => {
			console.log(data, '驾车的路线', 27)
			didiPath.distance = (data.paths[0].distance / 1000).toFixed(1)
			didiPath.minute = Math.ceil(data.paths[0].duration / 60)
			// 驾车的路线
			let points = []
			if (!data || !data.paths || !data.paths[0] || !data.paths[0].steps) {
				uni.$showMsg('查询失败')
				return
			}
			let steps = data.paths[0].steps
			for (let i = 0; i < steps.length; i++) {
				let polylines = steps[i].polyline.split(';')
				for (let j = 0; j < polylines.length; j++) {
					let locations = polylines[j].split(',')
					points.push({
						longitude: locations[0],
						latitude: locations[1]
					})
				}
			}
			// 路线的规划
			didiPath.polyline = [{
				points,
				color,
				width: 3
			}]

			if (this.polyline[0].points.length > 100) {
				// 改变起点
				this.points[0].longitude = this.polyline[0].points[5].longitude
				this.points[0].latitude = this.polyline[0].points[5].latitude
			} else if (this.polyline[0].points.length > 2) {
				// 改变起点
				this.points[0].longitude = this.polyline[0].points[2].longitude
				this.points[0].latitude = this.polyline[0].points[2].latitude
			} else if (didiPath.polyline[0].points.length == 2) {
				didiPath.carNum++
				// 改变起点
				didiPath.points[0].longitude = didiPath.polyline[0].points[1].longitude
				didiPath.points[0].latitude = didiPath.polyline[0].points[1].latitude

				if (didiPath.carNum == 2) {
					console.log('到达终点，行程结束')
					didiPath.initialPoint[0].longitude = didiPath.polyline[0].points[0].longitude
					didiPath.initialPoint[0].latitude = didiPath.polyline[0].points[0].latitude
					// 重置
					didiPath.carNum = 0
					return
				}
			}
			setTimeout(() => {
				didiPath.initialPoint[0].longitude = didiPath.polyline[0].points[0].longitude
				didiPath.initialPoint[0].latitude = didiPath.polyline[0].points[0].latitude

				didiPath.amapPlugin.getDrivingRoute(trendsPath(didiPath.points, didiPath))
			}, 500)
		},
		// 获取路线失败
		fail(info) {
			console.log('获取失败', info)
		}
	}
}

// 支付宝订单
function setTimeDateFmt(s) { // 个位数补齐十位数
	return s < 10 ? '0' + s : s
}

export function createordernum() {
	const now = new Date()
	let month = now.getMonth() + 1
	let day = now.getDate()
	let hour = now.getHours()
	let minutes = now.getMinutes()
	let seconds = now.getSeconds()
	month = setTimeDateFmt(month)
	day = setTimeDateFmt(day)
	hour = setTimeDateFmt(hour)
	minutes = setTimeDateFmt(minutes)
	seconds = setTimeDateFmt(seconds)
	let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math
		.random() * 1000000)).toString()
	return orderCode
	//基于年月日时分秒 + 随机数生成订单编号
}
